POV-Ray : Newsgroups : povray.advanced-users : Random points on surface of an object : Random points on surface of an object Server Time
6 Oct 2024 13:43:50 EDT (-0400)
  Random points on surface of an object  
From: Tim Attwood
Date: 22 Jan 2007 16:38:53
Message: <45b52eed$1@news.povray.org>
This morning I woke up with a solution
to placing a random distribution of points
onto arbitrary objects. I'm not sure if any
of you have ever had one of these dreaming
moments of inspiration, but I figured it was
worth sharing it. I think someone had asked
about something like this, but I've lost the
thread.

Basically, you just check that the distance
from a random point in the bounding to
a trace hit on the object is shorter than
some threshold distance. That way the
random distribution approaches that of
the surface.

Here's a sample scene, with the macro.

#macro RandOnSurface(Obj, rsd, thresh)
   #local mn = min_extent(Obj);
   #local mx = max_extent(Obj);
   #local dist = 1000000;
   #while (dist>thresh)
      #local flag = 0;
      #while (flag = 0)
         #local rfrom = VRand_In_Box(mn, mx, rsd);
         #local rdir = vrotate(<1,0,0>,
            <360*rand(rsd),360*rand(rsd),360*rand(rsd)>);
         #declare Norm = <0, 0, 0>;
         #local hit = trace(Obj, rfrom, rdir, Norm);
         #if (vlength(Norm) != 0)
            #local flag = 1;
         #end
      #end
      #local dist = vlength( hit - rfrom);
   #end
   #local result = hit;
   (result)
#end

#declare Bent = merge {
   cylinder{<0,-1,0>,<-1,0,0>,0.2}
   cylinder{<-1,0,0>,<1,0,0>,0.2}
   cylinder{<1,0,0>,<0,1,0>,0.2}
   sphere {<-1,0,0>,0.2}
   sphere {<1,0,0>,0.2}
};

object {Bent pigment {Blue transmit 0.7} }

#declare rsd = seed(1);

#local c = 0;
#while (c<1000)
   #local p = RandOnSurface(Bent, rsd, 0.05);
   cone{<0,0,0>,0.025,<0,0.1,0>,0
     Point_At_Trans(Norm)
     translate p
     pigment{Red}
   }
   #local c = c + 1;
#end


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.